package com.zh.mp.demo.config;

import com.zh.mp.demo.thread.MyThreadFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Configuration
@EnableScheduling
public class TaskConfig implements SchedulingConfigurer {

    protected SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        //taskRegistrar.setScheduler(taskExecutor());
        //taskRegistrar.setTaskScheduler(taskScheduler());
    }

    /**
     * 这里等同于配置文件配置
     * {@code spring.task.scheduling.pool.size=20} - Maximum allowed number of threads.
     * {@code spring.task.scheduling.thread-name-prefix=Job-Thread- } - Prefix to use for the names of newly created threads.
     * {@link org.springframework.boot.autoconfigure.task.TaskSchedulingProperties}
     */
    public Executor taskExecutor() {
        ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(20, new MyThreadFactory());
        scheduledExecutorService.scheduleAtFixedRate((Runnable) () -> {
            try {
                log.info(String.format("时间:%s",ft.format(new Date( ))));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, 3000, 3000, TimeUnit.MILLISECONDS);
        scheduledExecutorService.scheduleWithFixedDelay((Runnable) () -> {
            log.info(String.format("时间:%s",ft.format(new Date( ))));
        }, 3000, 3000, TimeUnit.MILLISECONDS);
        return scheduledExecutorService;
    }

    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.initialize();
        threadPoolTaskScheduler.setPoolSize(20);
        threadPoolTaskScheduler.setThreadNamePrefix("taskExecutor-");
        threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
        threadPoolTaskScheduler.setAwaitTerminationSeconds(60);
        String cron = "0/10 * * * * ? ";
        threadPoolTaskScheduler.schedule((Runnable) () -> {
            try {
                log.info(String.format("时间:%s",ft.format(new Date( ))));
                //log.info(String.format("调度器rpc调用完成,耗时:%s", watch.getLastTaskTimeMillis()));
            } catch (Exception ex) {
                log.error(String.format("调度器出现异常:%s", ex.getMessage()));
            }
        }, new CronTrigger(cron));

        return threadPoolTaskScheduler;
    }
}